package com.headwind.controller;

import cn.hutool.core.date.DateUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.headwind.entity.HwMood;
import com.headwind.service.HwMoodService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/10
 * @description : 访问控制类
 */
@Log4j2
@RestController
@RequestMapping("/multi")
public class HwMoodController {
    private final HwMoodService moodService;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    @Autowired
    public HwMoodController(HwMoodService moodService) {
        this.moodService = moodService;
    }

    @GetMapping("/find")
    public String findAll() throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() ->
                gson.toJson(moodService.findAll()));
        return future.get();
    }

    @PostMapping("/save")
    public String save(@RequestBody HwMood mood) throws ExecutionException, InterruptedException {
        mood.setPublishTime(DateUtil.date());
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() ->
                gson.toJson(moodService.save(mood)));
        log.info("说说发表成功,内容是:" + mood);
        return future.get();
    }
}
